package com.medicine.hbp.uitl;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.Toast;

public class ChoosePicturesUtils {

	private final static String TAG = "ChoosePicturesUtils";

	public final static int REQUEST_CODE_TAKE_PICTURE = 11;// �������ղ����ı�־
	public final static int REQUEST_CROP_ICON = 12; // ���ý�ͼ������
	public final static int PHOTOZOOM = 13;
	public static final String SDCARD_ROOT_PATH = Environment
			.getExternalStorageDirectory().getAbsolutePath();// ·��
	public static final String SAVE_PATH_IN_SDCARD = "/DCIM/Camera"; // ͼƬ���������ݱ����ļ���
	public static final String SAVE_PATH_CROP_SDCARD = "bhp/Pictures";
	public static String imagePathString = "";
	private static Uri imageFilePath;
	public static String IMAGE_CAPTURE_NAME = ""; // ��Ƭ����

	/**
	 * ѡ���ȡͼƬ�ķ�ʽ
	 */
	public static void chooseGetIMGMethod(final Context context) {
		final CharSequence[] items = { "�����ѡ��", "����" };
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle("ѡ��ͼƬ");
		builder.setItems(items, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int item) {
				Activity activity = (Activity) context;
				if (item == 1) {
					photographic(activity);
				} else {
					pickPhonoImage(activity);
				}
			}
		});
		AlertDialog alert = builder.create();
		alert.show();
	}

	/**
	 * ���洢���Ƿ����
	 * 
	 * @return
	 */
	public static boolean isHasSdcard() {
		String status = Environment.getExternalStorageState();
		if (status.equals(Environment.MEDIA_MOUNTED)) {
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * ����ϵͳ������
	 */
	public static void photographic(Activity activity) {
		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		// �洢������ ����Ƭ�洢�� sdcard -
		if (isHasSdcard()) {
			File cacheDir = new File(SDCARD_ROOT_PATH , SAVE_PATH_CROP_SDCARD);
			if (!cacheDir.exists()) {
				cacheDir.mkdirs();
			}
			IMAGE_CAPTURE_NAME = getImageName() + ".jpg";
			File imageFile = new File(cacheDir, IMAGE_CAPTURE_NAME);
			imagePathString = imageFile.getPath();
			intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile));
			intent.putExtra("return-data", true);

			Log.e(TAG, "imagePathString == " + imagePathString);
			activity.startActivityForResult(intent, REQUEST_CODE_TAKE_PICTURE);

		} else {
			Toast.makeText(activity, "����SD�����ڲ�����", Toast.LENGTH_SHORT).show();
		}
	}

	public static String getImageName() {
		Random random = new Random();
		int rad = random.nextInt(8999)+1000;
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd_HH-mm-ss_SSS");
		return dateFormat.format(new Date())+rad;
	}

	/**
	 * ����ϵͳͼ��
	 */
	public static void pickPhonoImage(Activity activity) {
		Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
		intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
				"image/*");
		activity.startActivityForResult(intent, PHOTOZOOM);
	}

	public static void cropPictures(Activity activity) {
		// Intent intent = new Intent("com.android.camera.action.CROP");
		// // intent.setClassName("com.android.camera",
		// "com.android.camera.CropImage");
		Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
		intent.setType("image/*");
		intent.putExtra("crop", "true");
		intent.putExtra("aspectX", 1);
		intent.putExtra("aspectY", 1);
		intent.putExtra("outputX", 96);
		intent.putExtra("outputY", 96);
		intent.putExtra("noFaceDetection", true);
		intent.putExtra("return-data", true);
		activity.startActivityForResult(intent, REQUEST_CROP_ICON);
	}

	public static void cropPictures(Activity activity, Uri uri) {
		System.out.println("uri==" + uri);
		Intent intent = new Intent("com.android.camera.action.CROP");

		intent.setDataAndType(uri, "image/*");
		File cacheDir = new File(SDCARD_ROOT_PATH, SAVE_PATH_CROP_SDCARD);
		if (!cacheDir.exists()) {
			cacheDir.mkdirs();
		}
		IMAGE_CAPTURE_NAME = getImageName() + "_crop" + ".jpg";
		File imageFile = new File(cacheDir, IMAGE_CAPTURE_NAME);
		imagePathString = imageFile.getPath();

		intent.putExtra("scale", true);
		intent.putExtra("crop", "true");// crop=true �������ܳ������Ĳü�ҳ��.
		intent.putExtra("aspectX", 1);// ������Ϊ�ü���ı���.
		intent.putExtra("aspectY", 1);// x:y=1:2
		int size = 200;
		System.out.println("size==" + size);
		intent.putExtra("outputX", size);
		intent.putExtra("outputY", size);
		intent.putExtra("scale", true);//�ڱ�
		intent.putExtra("scaleUpIfNeeded", true);//�ڱ�
		intent.putExtra("noFaceDetection", false);
		intent.putExtra("outputFormat", "JPEG");// ���ظ�ʽ
		intent.putExtra("return-data", true);
//		intent.putExtra("outputFormat",Bitmap.CompressFormat.JPEG.toString());
		intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile));
		activity.startActivityForResult(intent, REQUEST_CROP_ICON);
	}
	/**
	 * ͼƬ�ü�
	 * @param activity
	 * @param mUri
	 */
//	public static void cropPictures(Activity activity, Uri mUri) {
//		if(null == mUri)return;  
//		Intent intent = new Intent();  
//		intent.setAction("com.android.camera.action.CROP");  
//		intent.setDataAndType(mUri, "image/*");// mUri���Ѿ�ѡ���ͼƬUri  
//		intent.putExtra("crop", "true");  
//		intent.putExtra("aspectX", 1);// �ü������  
//		intent.putExtra("aspectY", 1);  
//		intent.putExtra("outputX", 200);// ���ͼƬ��С  
//		intent.putExtra("outputY", 200);  
//		intent.putExtra("return-data", true);  
//		activity.startActivityForResult(intent, REQUEST_CROP_ICON);
//	}
//	
	public static boolean saveBytes(byte[] bytes){
		boolean flag = true;
		File cacheDir = new File(SDCARD_ROOT_PATH, SAVE_PATH_CROP_SDCARD);
		if (!cacheDir.exists()) {
			cacheDir.mkdirs();
		}
		IMAGE_CAPTURE_NAME = getImageName() + "_crop" + ".jpg";
		File imageFile = new File(cacheDir, IMAGE_CAPTURE_NAME);
		imagePathString = imageFile.getPath();
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(imageFile);
			fos.write(bytes);
			fos.flush();
		} catch (Exception e) {
			flag = false;
			e.printStackTrace();
		}finally{
			try {
				if(fos!=null){
					fos.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return flag;
	}
	public static String saveBytes1(byte[] bytes){
		File cacheDir = new File(SDCARD_ROOT_PATH, SAVE_PATH_CROP_SDCARD);
		if (!cacheDir.exists()) {
			cacheDir.mkdirs();
		}
		IMAGE_CAPTURE_NAME = getImageName() + "_crop" + ".jpg";
		File imageFile = new File(cacheDir, IMAGE_CAPTURE_NAME);
		imagePathString = imageFile.getPath();
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(imageFile);
			fos.write(bytes);
			fos.flush();
		} catch (Exception e) {
			e.printStackTrace();
			imagePathString = "";
		}finally{
			try {
				if(fos!=null){
					fos.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return imagePathString;
	}
	
	/**
	 * ����ͷ��
	 * @param bytes
	 * @return
	 */
	public static String saveBytes2(ByteArrayOutputStream baos){
		String image_path = "";
		File cacheDir = new File(SDCARD_ROOT_PATH, SAVE_PATH_CROP_SDCARD);
		if (!cacheDir.exists()) {
			cacheDir.mkdirs();
		}
		String IMAGE_CAPTURE_NAME = getImageName() + "_crop" + ".jpg";
		File imageFile = new File(cacheDir, IMAGE_CAPTURE_NAME);
		image_path = imageFile.getPath();
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(imageFile);
			fos.write(baos.toByteArray());
			fos.flush();
		} catch (Exception e) {
			e.printStackTrace();
			image_path = "";
		}finally{
			try {
				if(baos!=null){
					baos.close();
				}
				if(fos!=null){
					fos.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return image_path;
	}
	
	/**
	 * ����ѹ��ͼƬ
	 * @param baos
	 * @param picSuffix ͼƬ��׺����jpg
	 * @return
	 */
	public static String saveBytes3(ByteArrayOutputStream baos,String picSuffix){
		String image_path = "";
		File cacheDir = new File(SDCARD_ROOT_PATH, SAVE_PATH_CROP_SDCARD);
		if (!cacheDir.exists()) {
			cacheDir.mkdirs();
		}
		String IMAGE_CAPTURE_NAME = getImageName() + "_crop" + "."+picSuffix;
		File imageFile = new File(cacheDir, IMAGE_CAPTURE_NAME);
		image_path = imageFile.getPath();
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(imageFile);
			fos.write(baos.toByteArray());
			fos.flush();
		} catch (Exception e) {
			e.printStackTrace();
			image_path = "";
		}finally{
			try {
				if(baos!=null){
					baos.close();
				}
				if(fos!=null){
					fos.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return image_path;
	}
}
